//index.js
//获取应用实例
var dis = require("../../utils/distance.js")
var oth = require('../../utils/other.js')
var com = require('../../utils/common.js')
const app = getApp()
Page({
  data: {
    mode: "scaleToFill",
    arr: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    attendClassColor: null, //控制仅上课图片颜色
    venues: [], //场馆数组
    coach: [],
    spinShow:true //加载控制
  },
  onLoad: function(options) {
    console.log(123)
    console.log(options)
    var that = this;
    com.autoReg()   
    wx.request({
        url: app.url + 'weiapp/api/indexBanner',
        data: {},
        success: function(res) {
          console.log(res.data)
          that.setData({
            arr: res.data
          })
        }
      }),
      wx.request({
        url: app.url + 'weiapp/api/venues',
        data: {},
        success: function(res) {
          console.log(app.globalData.location)
          for(var index in res.data)
          {
            var distance = dis.getDistance(app.globalData.location['latitude'], app.globalData.location['longitude'], res.data[index]['lat'], res.data[index]['lng'])
            res.data[index]['distance'] = distance
            res.data[index]['juli'] = "距离约" + parseInt(distance / 1000) + "Km";
          }
          console.log(res.data)
          let arr = oth.sort(res.data)
          that.setData({
            venues: arr
          })
        }
      }),
      wx.request({
        url: app.url + 'weiapp/api/coachs',
        data: {},
        success: function(res) {
          console.log(res.data)
          that.setData({
            coach: res.data,
          })
        }
      })
      setTimeout(function(){
        that.setParent()
      },3000)
      if(!wx.getStorageSync('userinfo'))
      {
        wx.request({
          url: app.url + "/weiapp/api/userinfo",
          data: {
            uid: wx.getStorageSync('uid')
          },
          success: function (res) {
            wx.setStorageSync('userinfo', res.data.data)
          }
        })
      }
  },
  onShow:function(){
  },
  goStoreDetail(event) {
//    console.log(event)
    wx.navigateTo({
      url: '../store_detail/store_detail?id=' + event.currentTarget.dataset.id,
    })
  },

  goVenueList() {
    wx.navigateTo({
      url: "../venue_list/venue_list",
    })
  },

  attendClass() {
    console.log(this.data.attendClassColor)
    if (this.data.attendClassColor == null) {
      this.setData({
        attendClassColor: 'red'
      })
    } else {
      this.setData({
        attendClassColor: null
      })
    }
  },
  goCoachList(){
    wx.navigateTo({
      url: '../coach_list/coach_list',
    })
  },
  goCoachGoods(even){
    wx.navigateTo({
      url: '../coach_goods/coach_goods?id=' + even.currentTarget.dataset.coach_id,
    })

  },
  goNews(e){
    console.log(e)
    let url = e.currentTarget.dataset.url
    if(url == 'card')
    {
      wx.switchTab({
        url: '../buy_card/buy_card',
      })
      return;
    }
    if(url == 'intro')
    {
      wx.navigateTo({
        url: '../img_intro/img_intro'
      })
      return;
    }
    wx.setStorageSync('url', url)
    wx.navigateTo({
      url: '../news/news',
    })
    return;
  },
  

  onShareAppMessage: function (res) {
    // if (res.from === 'button') {
    //   // 来自页面内转发按钮
    //   console.log(res.target)
    // }
    // return {
    //   title: '联齐运动',
    //   path: '/pages/guide/guide?uid='+wx.getStorageSync('uid')
    // }
  },
  setParent: function () {
    wx.request({
      url: app.url + 'weiapp/api/setParent',
      data: {
        parent: wx.getStorageSync('parent'),
        PHPSESSID: wx.getStorageSync('PHPSESSID')
      },
      success: function (res) {
        console.log(res)
      }
    })
  }  
})
